package com.hhd.flow.controller;

import com.hhd.flow.engine.FlowEngine;
import com.hhd.flow.engine.cmd.impl.AddNotifyCommand;
import com.hhd.flow.engine.cmd.impl.QueryNotifyFlowUserCommand;
import com.hhd.flow.vo.AddNotifyVo;
import com.hhd.flow.vo.QueryNotifyFlowUserVo;
import com.hhd.flow.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/flow")
public class AddNotifyController {
    @Autowired
    FlowEngine flowEngine;

    @PostMapping("/addNotify")
    @Transactional
    public Result addNotify(@RequestBody AddNotifyVo addNotifyVo) {
        AddNotifyCommand command = flowEngine.newAddNotifyCommandBuilder()
                .comment(addNotifyVo.getComment())
                .flowUsers(addNotifyVo.getFlowUsers())
                .flowUserTaskId(addNotifyVo.getFlowUserTaskId())
                .commentInfo(addNotifyVo.getCommentInfo())
                .build();
        flowEngine.execute(command);
        return Result.ok();
    }

    @PostMapping("/queryNotifyFlowUser")
    public Result queryNotifyFlowUser(@RequestBody QueryNotifyFlowUserVo queryNotifyFlowUserVo) {
        QueryNotifyFlowUserCommand command = flowEngine.newQueryNotifyFlowUserCommandBuilder()
                .userTask(queryNotifyFlowUserVo.getUserTask())
                .flowUserTaskId(queryNotifyFlowUserVo.getFlowUserTaskId())
                .build();
        return Result.ok(flowEngine.execute(command).getResult());
    }
}
